package com.itheima.remark.service.impl;

import com.itheima.remark.dao.ProductMapper;
import com.itheima.remark.service.ProductService;
import com.itheima.pojo.Product;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.remark.search.pojo.RemarkImfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Product> findAll() {
        return productMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Product findById(Integer id){
        return  productMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     * @param product
     */
    @Override
    public void add(Product product){
        productMapper.insert(product);
    }


    /**
     * 修改
     * @param product
     */
    @Override
    public void update(Product product){
        productMapper.updateByPrimaryKey(product);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        productMapper.deleteByPrimaryKey(id);
    }


    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Product> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return productMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Product> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Product>)productMapper.selectAll();
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Product> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Product>)productMapper.selectByExample(example);
    }

    @Override
    public Page<RemarkImfo> findInfoPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<RemarkImfo>) productMapper.selectAllRemarkImfo();
    }

    @Override
    public Integer count() {
       return productMapper.selectCountAll();
    }

    @Override
    public RemarkImfo findInfoBtId(Integer id) {
        return productMapper.selectInfoById(id);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // name
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 开店时间存json
            if(searchMap.get("time_scpe")!=null && !"".equals(searchMap.get("time_scpe"))){
                criteria.andLike("time_scpe","%"+searchMap.get("time_scpe")+"%");
           	}
            // images
            if(searchMap.get("images")!=null && !"".equals(searchMap.get("images"))){
                criteria.andLike("images","%"+searchMap.get("images")+"%");
           	}

            // id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // category_id
            if(searchMap.get("categoryId")!=null ){
                criteria.andEqualTo("categoryId",searchMap.get("categoryId"));
            }
            // 星级
            if(searchMap.get("score")!=null ){
                criteria.andEqualTo("score",searchMap.get("score"));
            }
            // money
            if(searchMap.get("money")!=null ){
                criteria.andEqualTo("money",searchMap.get("money"));
            }
            // address_id
            if(searchMap.get("addressId")!=null ){
                criteria.andEqualTo("addressId",searchMap.get("addressId"));
            }

        }
        return example;
    }

}
